package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;


import com.itheima.pojo.Role;

import com.itheima.pojo.User;
import com.itheima.service.UserManageService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 用户控制层:
 */
@RestController
@RequestMapping("/usermanage")
public class UserManageController {
    //订阅服务
    @Reference
    private UserManageService userManageService;

    /**
     * 查询所有角色数据
     */
    @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    public Result findAll() {
        try {
            List<Role> userManageList = userManageService.findAll();
            return new Result(true, MessageConstant.QUERY_USERMANAGE_SUCCESS, userManageList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_USERMANAGE_FAIL);
        }
    }

    /**
     * 新增用户管理
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result add(@RequestBody User userManage, Integer[] userManageIds) {
        try {
            userManageService.add(userManage, userManageIds);
            return new Result(true, MessageConstant.ADD_USERMANAGE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_USERMANAGE_FAIL);
        }
    }

    /**
     * 实现功能:分页查询
     *
     * @param queryPageBean
     * @return
     */
    @RequestMapping(value = "/findPage", method = RequestMethod.POST)
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return userManageService.findPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());

    }

    /**
     * 功能实现:删除一个
     * @param userManageId
     * @return
     */
    @RequestMapping(value = "/deleteById", method = RequestMethod.GET)
    public Result deleteById(Integer userManageId) {
        try {
            userManageService.deleteById(userManageId);
            return new Result(true, MessageConstant.DELETE_USERMANAGE_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_USERMANAGE_FAIL);
        }
    }

    /**
     * 根据用户id查询用户
     */
    @RequestMapping(value = "/findById", method = RequestMethod.GET)
    public Result findById(Integer userManageId) {
        try {
            User user = userManageService.findById(userManageId);
            //tempPassword = user.getPassword();
            user.setPassword("***");
            return new Result(true, MessageConstant.QUERY_USERMANAGE_SUCCESS2, user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.QUERY_USERMANAGE_FAIL2);
        }
    }

    /*
    根据用户id 查询角色中间表
    获取角色ids
     */
    @RequestMapping(value = "/findRoleIdsByUserManageId", method = RequestMethod.GET)
    public List<Integer> findRoleIdsByUserManageId(Integer userManageId) {

        return userManageService.findRoleIdsByUserManageId(userManageId);

    }

    /**
     * 编辑用户
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public Result edit(@RequestBody User userManage, Integer[] userManageIds) {
        try {
            userManageService.edit(userManage, userManageIds);
            return new Result(true, MessageConstant.EDIT_USERMANAGE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_USERMANAGE_FAIL);
        }
    }
}
